<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Product
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for product
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Product
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Product extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'product';

    /**
     * Save product
     * @param array $row 
     * @return integer
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $id = $this->insert($row);
        }
        return $id;
    }

    /**
     * Find ID of product by content ID
     * @param integer $contentId
     * @return integer 
     */
    public function findIdByContentId($contentId)
    {
        // find product by content ID in database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        $query->where('content_id = ?', $contentId);
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Get products by IDs
     * @param array $ids 
     * @return array
     */
    public function getByIds($ids, $languageId)
    {
        $result = array();
        if ($ids) {
            $query = $this->getAdapter()->select();
            $query->from(array('p' => $this->_name), array('p.id', 'p.mpn', 'p.name as name', 'p.thumb_pic_url', 'p.price'));
            $query->join(array('m' => 'manufacturer'), 'm.id = p.manufacturer_id', 'm.name AS manufacturer_name');
            $query->join(array('c' => 'category'), 'c.id = p.category_id', 'c.id as category_id');
            $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', 'ct.name AS category_name');
            $query->where('p.id IN (' . implode(',', $ids) . ')');
            $query->where('ct.language_id = ?', $languageId);
            $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
            foreach ($result as $row) {
                $row->category_name = mb_strtoupper(mb_substr($row->category_name, 0, 1, 'utf8'), 'utf8') . mb_substr($row->category_name, 1, 255, 'utf8');
            }
        }
        return $result;
    }

    /**
     * Get list of IDs for price rule
     * @param integer $productId
     * @param integer $categoryId
     * @param integer $manufacturerId
     */
    public function getIdsForPriceRule($productId, $categoryId, $manufacturerId)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        if ($productId) {
            $query->where('id = ?', $productId);
        }
        if ($categoryId) {
            $query->where('category_id = ?', $categoryId);
        }
        if ($manufacturerId) {
            $query->where('manufacturer_id = ?', $manufacturerId);
        }
        $result = $this->getAdapter()->fetchCol($query);
        return $result;
    }

}
